(function(){

'use strict';
/**
 * customer 吴尧
 * @ngdoc function
 * @name sasdApp.controller:trafficAnalysisController
 * @description 客户发展分析控制器
 * # trafficAnalysisController
 * Controller of the sasApp
 */
angular.module('controllers.oldOMDisplay.infoDispaly')
  .controller('trafficAnalysisController',trafficAnalysisController);
function trafficAnalysisController($scope,InfoViewService,COREDATA,CoreService,BankAtaServer,$filter,CoreLoading){
	var paramsData = {};
	$scope.errorInfo = COREDATA.ERRORINFO;   //日期错误信息
	$scope.shoumun = 1;
	$scope.provinces =COREDATA.PROVINCES;
	$scope.timeTypes = COREDATA.TIMETYPES; //帐期数据

	$scope.translate = {
		provinceCode: [],
		businessCode: '01',
		timeTypeCode: {timeTypeCode: '1',timeTypeName: '日'},
		startTime: new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate() -31),
		startQuarter: { code: '1', quarterName: '第一季度'},
		endTime: new Date(),
		endQuarter: { code: '1', quarterName: '第一季度'},
		year: {}
	};
	/**
	 * [ 设置账期起始时间的默认值]
	 * 初始化页面值的默认 查询账期为日
	 * @param {[type]} timeTypeCode [description]
	 */
	$scope.$watch('translate.timeTypeCode',function(newValue){
		var month = new Date().getMonth();                  //当前月份
		var day = new Date().getDate();                     //当前天
		var year = new Date().getFullYear();                //当前年份
		switch (newValue.timeTypeCode) {
			case '1':
					$scope.translate.startTime = new Date(year, month, day - 31);
					$scope.translate.endTime = new Date(year, month, day - 1);
				break;
			case '3':
				$scope.translate.startTime = new Date(year, month-5);
				$scope.translate.endTime = new Date(year, month);
				break;
			case '4':
					$scope.translate.year.startTime = '20141';
					$scope.translate.year.endTime = '20152';
				break;
			case '5':
					$scope.translate.year.startTime = Number(new Date().getFullYear()-5);
					$scope.translate.year.endTime = Number(new Date().getFullYear());
		}
	});
	var sysCode = { systemCode: '11' };
  $scope.queryBusiness = function(){
	  if ($scope.businessList && $scope.businessList.length !== 0) {
		  return;
	  }
	  InfoViewService.queryBusiness(sysCode,function(res){
		  res.state === '10000' ? $scope.businessList = res.data : CoreService.dialog(res.state);
	  });
  };

	/**
	 * 季度字符串,解析为年度和季度字段
	 * @param quarterString 季度字符串
	 * @returns {{}} 返回季度，年份的对象
	 */
	function splitTime(quarterString){
		var yearQuarter ={};
		var year = quarterString.substring(0,4);
		var quarter = quarterString.substring(4,5);
		yearQuarter.year = year;
		yearQuarter.quarter = quarter;
		return yearQuarter;
	}

	$scope.queryTraffic = function() {
		$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
		var queryInfo = angular.copy($scope.translate);
		switch(queryInfo.timeTypeCode.timeTypeCode){
			case '1':
				paramsData.startTime = CoreService.dateFormat(queryInfo.startTime, 'yyyy-MM-dd');
				paramsData.endTime = CoreService.dateFormat(queryInfo.endTime, 'yyyy-MM-dd');
				break;
			case '3':
				paramsData.startTime = CoreService.dateFormat(queryInfo.startTime, 'yyyy-MM')+'-01';
				paramsData.endTime = CoreService.dateFormat(queryInfo.endTime, 'yyyy-MM')+'-01';
				break;
			case '4':
				var start = splitTime(queryInfo.year.startTime);
				var end = splitTime(queryInfo.year.endTime);
				paramsData.startTime = start.year;
				paramsData.startQuarter = start.quarter;
				paramsData.endTime = end.year;
				paramsData.endQuarter = end.quarter;
				break;
			case '5':
				paramsData.startTime = CoreService.dateFormat(queryInfo.startTime, 'yyyy')+'-01-01';
				paramsData.endTime = CoreService.dateFormat(queryInfo.endTime, 'yyyy')+'-01-01';
				break;
		}
        paramsData.provinceCodes = $scope.translate.provinceCode.map(function(item){
          return item.provinceCode;
        }); 
        paramsData.timeType = $scope.translate.timeTypeCode.timeTypeCode;
        paramsData.businessCode = $scope.translate.businessCode;
		if($scope.shoumun === 0){
			CoreLoading.setState(true);
			InfoViewService.queryTraffic(paramsData, queryTraffic);
		}

	};
	var title,searchTime,businessNum,provinceName,businessName;
	function queryTraffic(res) {
		provinceName = $scope.translate.provinceCode.length === 0 ? '全国': $scope.translate.provinceCode.map(function(item){
		  return item.provinceName;
		}).join('、');
		if($scope.translate.businessCode === '01'){
            businessName = '缴费';
		}else if($scope.translate.businessCode === '02'){
			businessName ='签约';
		}else{
			businessName = '解约';
		}

		title = provinceName+businessName+'客户业务量统计';
		searchTime = [];
		businessNum =[];
		angular.forEach(res.data, function(item){
			searchTime.push(item.time);
			businessNum.push(item.businessNum);
		});

		CoreLoading.setState(false);
		if (res.data && res.data.length >0) {
		   $scope.flagObj = true;

			var customerEchart = echarts.init(document.getElementById('traffic'));

			var options = {
				title: {  text: title,  x: 'center'},
				grid: {x:'10%',  x2: '10%'},
				tooltip: {   trigger: 'axis' },
				legend: {  orient: 'vertical',  x: 'right',  y: 'center',  data: ['客户业务量']},
				xAxis: [{  type: 'category',  data: searchTime}],
				yAxis: [{   name: '(笔)',   axisLine: {     onZero: false   },   type: 'value' }],
				series: [
					{ name: '客户业务量',type: 'line',stack: '总量',data: businessNum}],
				datazoom:[1,20]
			};

			customerEchart.setOption(options);

		}else{
			$scope.flag = 'hidden';
			CoreService.dialog('数据不存在');
		}
	}
	$scope.queryTraffic();



}

})();

